<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>01 - ol4加载浙江政务地图</title>
    <script src="../lib/ol4/ol4.js"></script>
    <script src="../lib/proj4/proj4.js"></script>
    <link rel="stylesheet" href="../css/ol4/ol.css">
    <link rel="stylesheet" href="../css/main.css">
</head>
<body>
<div id="map"></div>
</body>
<script>
    const projection4490 = new ol.proj.Projection({
        code: 'EPSG:4490',
        extent: [-180, -90, 180, 90],
        units: 'degrees',
        axisOrientation: 'neu'
    });
    proj4.defs('EPSG:4490', '+proj=longlat +ellps=GRS80 +no_defs');
    ol.proj.addProjection(projection4490);
    ol.proj.addCoordinateTransforms('EPSG:4326', 'EPSG:4490', coordinate => {
        return proj4('EPSG:4326', 'EPSG:4490', coordinate);
    }, (coordinate => {
        return proj4('EPSG:4490', 'EPSG:4326', coordinate);
    }));

    let projection = new ol.proj.get('EPSG:4490');
    const tileSize = 256;
    const projectionExtent = projection.getExtent();
    console.log(projectionExtent);

    const origin = [-180, 90];
    console.log('origin:', origin);

    const size = ol.extent.getWidth(projectionExtent) / tileSize;
    const resolutions = new Array(20);
    const matrixIds = new Array(20);
    for (let z = 1; z < 21; z++) {
        resolutions[z] = size / Math.pow(2, z);
        matrixIds[z] = z;
    }


    const tileGrid = new ol.tilegrid.WMTS({
        origin,
        resolutions: resolutions,
        extent: projectionExtent,
        matrixIds: matrixIds
    });

    const url = `http://ditu.zjzwfw.gov.cn/mapserver/vmap/WMTS/1.0.0/zjvmap/tdt_biaozhunyangshi_2017?token=ef6550d1-b50d-422d-a752-c44aae4bfccf`;
    const url1 = `http://ditu.zjzwfw.gov.cn/mapserver/label/WMTS/1.0.0/zjvmap/tdt_biaozhunyangshi_2017`;


    const wmtsSource = new ol.source.WMTS({
        url,
        layer: 'zjvmap',
        matrixSet: 'default028mm',
        format: 'image/png',
        projection: projection,
        tileGrid: tileGrid,
        style: 'tdt_biaozhunyangshi_2017'
        // wrapX: false
    });

    const wmtsSource1 = new ol.source.WMTS({
        url: url1,
        layer: 'zjvmap',
        matrixSet: 'default028mm',
        format: 'image/png',
        projection: projection,
        tileGrid: tileGrid,
        style: 'tdt_biaozhunyangshi_2017'
        // wrapX: false
    });
    // 图层
    const baseLayer = new ol.layer.Tile({
        source: wmtsSource
    });

    const baseLayer1 = new ol.layer.Tile({
        source: wmtsSource1
    });

    // const circlePoint = ol.proj.transform([116.384722, 39.889916], 'EPSG:4326', 'EPSG:3857');
    // console.log(circlePoint);
    const points = [
        [120.15, 30.268]
    ];
    const centerPoint = points[0];

    const iconFeature = new ol.Feature({
        geometry: new ol.geom.Point(centerPoint)
    });

    const iconStyle = new ol.style.Style({
        image: new ol.style.Icon({
            anchor: [10, 26],
            anchorXUnits: 'pixels',
            anchorYUnits: 'pixels',
            src: '../image/position_orange.png'
        })
    });

    iconFeature.setStyle(iconStyle);

    const vectorSource = new ol.source.Vector({
        features: [iconFeature],
        wrapX: false
    });

    const iconLayer = new ol.layer.Vector({
        source: vectorSource
    });

    const map = new ol.Map({
        layers: [
            baseLayer,
            baseLayer1,
            iconLayer
        ],
        target: 'map',
        view: new ol.View({
            center: centerPoint,
            projection,
            zoom: 10,
            maxZoom: 19,
            minZoom: 1,
            resolutions
        })
    });
</script>
</html>